﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Web.Profile;

namespace VASJ.Provider
{
    public class AcpInetProfileProvider : ProfileProvider
    {
        BasicHttpBinding _binding;
        EndpointAddress _endpoint;

        string _ApplicationName = string.Empty;

        string username = string.Empty;
        string password = string.Empty;

        /// <summary>
        /// Init
        /// </summary>
        public AcpInetProfileProvider()
        {
            BaseHelper.GetConfig(out _binding, out _endpoint, out _ApplicationName, out username, out password);
        }

        /// <summary>
        /// ApplicationName
        /// </summary>
        public override string ApplicationName
        {
            get
            {
                return this._ApplicationName;
            }
            set
            {
                this._ApplicationName = value;
            }
        }

        /// <summary>
        /// DeleteInactiveProfiles
        /// </summary>
        /// <param name="authenticationOption"></param>
        /// <param name="userInactiveSinceDate"></param>
        /// <returns></returns>
        public override int DeleteInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.DeleteInactiveProfiles(authenticationOption, userInactiveSinceDate);
        }

        /// <summary>
        /// DeleteProfiles
        /// </summary>
        /// <param name="usernames"></param>
        /// <returns></returns>
        public override int DeleteProfiles(string[] usernames)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.DeleteProfiles(usernames);
        }

        /// <summary>
        /// DeleteProfiles
        /// </summary>
        /// <param name="profiles"></param>
        /// <returns></returns>
        public override int DeleteProfiles(ProfileInfoCollection profiles)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.DeleteProfilesCollection(profiles);
        }

        /// <summary>
        /// FindInactiveProfilesByUserName
        /// </summary>
        /// <param name="authenticationOption"></param>
        /// <param name="usernameToMatch"></param>
        /// <param name="userInactiveSinceDate"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecords"></param>
        /// <returns></returns>
        public override ProfileInfoCollection FindInactiveProfilesByUserName(ProfileAuthenticationOption authenticationOption, string usernameToMatch, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.FindInactiveProfilesByUserName(out totalRecords, authenticationOption, usernameToMatch, userInactiveSinceDate, pageIndex, pageSize);
        }

        /// <summary>
        /// FindProfilesByUserName
        /// </summary>
        /// <param name="authenticationOption"></param>
        /// <param name="usernameToMatch"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecords"></param>
        /// <returns></returns>
        public override ProfileInfoCollection FindProfilesByUserName(ProfileAuthenticationOption authenticationOption, string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.FindProfilesByUserName(out totalRecords, authenticationOption, usernameToMatch, pageIndex, pageSize);
        }

        /// <summary>
        /// GetAllInactiveProfiles
        /// </summary>
        /// <param name="authenticationOption"></param>
        /// <param name="userInactiveSinceDate"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecords"></param>
        /// <returns></returns>
        public override ProfileInfoCollection GetAllInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.GetAllInactiveProfiles(out totalRecords, authenticationOption, userInactiveSinceDate, pageIndex, pageSize);
        }

        /// <summary>
        /// GetAllProfiles
        /// </summary>
        /// <param name="authenticationOption"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecords"></param>
        /// <returns></returns>
        public override ProfileInfoCollection GetAllProfiles(ProfileAuthenticationOption authenticationOption, int pageIndex, int pageSize, out int totalRecords)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.GetAllProfiles(out totalRecords, authenticationOption, pageIndex, pageSize);
        }

        /// <summary>
        /// GetNumberOfInactiveProfiles
        /// </summary>
        /// <param name="authenticationOption"></param>
        /// <param name="userInactiveSinceDate"></param>
        /// <returns></returns>
        public override int GetNumberOfInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.GetNumberOfInactiveProfiles(authenticationOption, userInactiveSinceDate);
        }

        /// <summary>
        /// GetPropertyValues
        /// </summary>
        /// <param name="context"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        public override System.Configuration.SettingsPropertyValueCollection GetPropertyValues(System.Configuration.SettingsContext context, System.Configuration.SettingsPropertyCollection collection)
        {
            return null;
        }

        /// <summary>
        /// SetPropertyValues
        /// </summary>
        /// <param name="context"></param>
        /// <param name="collection"></param>
        public override void SetPropertyValues(System.Configuration.SettingsContext context, System.Configuration.SettingsPropertyValueCollection collection)        
        {            
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.SetApplicationName(this._ApplicationName);
        }
    }

    public class AcpInetProfileHelper : BaseHelper
    {
        Dictionary<string, object> pfa = new Dictionary<string, object>();

        private string _username;

        /// <summary>
        /// Init
        /// </summary>
        /// <param name="username"></param>
        public AcpInetProfileHelper(string username)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            _username = username;
            pfa = acpiNet.GetProfile(this._username);
        }

        /// <summary>
        /// UpdateProfile
        /// </summary>
        /// <param name="info"></param>
        public void UpdateProfile(Dictionary<string, object> info)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            Dictionary<string, object> info_webservice = new Dictionary<string, object>();

            foreach (KeyValuePair<string, object> it in info)
            {
                switch (it.Value.GetType().ToString())
                {
                    case "AcpInetModel.PisaraProfile":
                        AcpInetModel.PisaraProfile ppf = (AcpInetModel.PisaraProfile)it.Value;
                        //Convert
                        if (!info_webservice.ContainsKey("Email")) info_webservice.Add("Email", ppf.Email);
                        if (!info_webservice.ContainsKey("Name")) info_webservice.Add("Name", ppf.FullName);
                        if (!info_webservice.ContainsKey("Organization")) info_webservice.Add("Organization", ppf.Organization);
                        if (!info_webservice.ContainsKey("Phone")) info_webservice.Add("Phone", ppf.Phone);
                        break;
                    case "AcpInetModel.LansimetroProfile":
                        AcpInetModel.LansimetroProfile lpf = (AcpInetModel.LansimetroProfile)it.Value;
                        //Convert
                        if (!info_webservice.ContainsKey("Email")) info_webservice.Add("Email", lpf.Email);
                        if (!info_webservice.ContainsKey("Name")) info_webservice.Add("Name", lpf.FullName);
                        if (!info_webservice.ContainsKey("Organization")) info_webservice.Add("Organization", lpf.Organization);
                        if (!info_webservice.ContainsKey("Phone")) info_webservice.Add("Phone", lpf.Phone);
                        break;
                    default:
                        if (!info_webservice.ContainsKey(it.Key)) info_webservice.Add(it.Key, it.Value);
                        break;
                }
            }

            acpiNet.CreateProfile(this._username, info_webservice);
        }

        /// <summary>
        /// GetPropertyValue
        /// </summary>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public object GetPropertyValue(string propertyName)
        {
            object retVal = null;

            switch (propertyName)
            {
                case "PisaraProfile":
                    AcpInetModel.PisaraProfile ppf = new AcpInetModel.PisaraProfile();
                    if (pfa.ContainsKey("Name")) ppf.FullName = Convert.ToString(pfa["Name"]);
                    if (pfa.ContainsKey("Email")) ppf.Email = Convert.ToString(pfa["Email"]);
                    if (pfa.ContainsKey("Organization")) ppf.Organization = Convert.ToString(pfa["Organization"]);
                    if (pfa.ContainsKey("Phone")) ppf.Phone = Convert.ToString(pfa["Phone"]);
                    retVal = ppf;
                    break;
                case "LansimetroProfile":
                    AcpInetModel.LansimetroProfile lpf = new AcpInetModel.LansimetroProfile();
                    if (pfa.ContainsKey("Name")) lpf.FullName = Convert.ToString(pfa["Name"]);
                    if (pfa.ContainsKey("Email")) lpf.Email = Convert.ToString(pfa["Email"]);
                    if (pfa.ContainsKey("Organization")) lpf.Organization = Convert.ToString(pfa["Organization"]);
                    if (pfa.ContainsKey("Phone")) lpf.Phone = Convert.ToString(pfa["Phone"]);
                    retVal = lpf;
                    break;
                default:
                    if (pfa.ContainsKey(propertyName)) retVal = pfa[propertyName];
                    break;
            }

            return retVal;
        }
    }
}